<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateBonusRequest;
use App\Http\Requests\UpdateBonusRequest;
use App\Models\Bonus;
use App\Repositories\BonusRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Str;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class BonusController extends AppBaseController
{
    /** @var  BonusRepository */
    private $bonusRepository;

    public function __construct(BonusRepository $bonusRepo)
    {
        $this->bonusRepository = $bonusRepo;
    }

    /**
     * Display a listing of the Bonus.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->bonusRepository->pushCriteria(new RequestCriteria($request));
        $bonuses = $this->bonusRepository->paginate(20);

        return view('bonuses.index')
            ->with('bonuses', $bonuses);
    }

    /**
     * Show the form for creating a new Bonus.
     *
     * @return Response
     */
    public function create()
    {
        return view('bonuses.create');
    }

    /**
     * Store a newly created Bonus in storage.
     *
     * @param CreateBonusRequest $request
     *
     * @return Response
     */
    public function store(CreateBonusRequest $request)
    {
        $input = $request->all();
        for($i=0;$i<$input['number'];$i++){
            $_input = $input;
            $_input['sn'] = date('YmdHis').mt_rand(1000,9999);
            $this->bonusRepository->create($_input);
        }

        Flash::success('Bonus saved successfully.');

        return redirect(route('bonuses.index'));
    }

    /**
     * Display the specified Bonus.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $bonus = $this->bonusRepository->findWithoutFail($id);

        if (empty($bonus)) {
            Flash::error('Bonus not found');

            return redirect(route('bonuses.index'));
        }

        return view('bonuses.show')->with('bonus', $bonus);
    }

    /**
     * Show the form for editing the specified Bonus.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $bonus = $this->bonusRepository->findWithoutFail($id);

        if (empty($bonus)) {
            Flash::error('Bonus not found');

            return redirect(route('bonuses.index'));
        }

        return view('bonuses.edit')->with('bonus', $bonus);
    }

    /**
     * Update the specified Bonus in storage.
     *
     * @param  int              $id
     * @param UpdateBonusRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateBonusRequest $request)
    {
        $bonus = $this->bonusRepository->findWithoutFail($id);

        if (empty($bonus)) {
            Flash::error('Bonus not found');

            return redirect(route('bonuses.index'));
        }

        $bonus = $this->bonusRepository->update($request->all(), $id);

        Flash::success('Bonus updated successfully.');

        return redirect(route('bonuses.index'));
    }

    /**
     * Remove the specified Bonus from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $bonus = $this->bonusRepository->findWithoutFail($id);

        if (empty($bonus)) {
            Flash::error('Bonus not found');

            return redirect(route('bonuses.index'));
        }

        $this->bonusRepository->delete($id);

        Flash::success('Bonus deleted successfully.');

        return redirect(route('bonuses.index'));
    }

    public function statistics()
    {
        $cates = Bonus::groupBy('name')->get();
        foreach ($cates as $cate) {
            $cate->use_number = Bonus::where('name', $cate->name)->where('use_status', 1)->count();
            $cate->allot_number = Bonus::where('name', $cate->name)->where('user_id', '>',0)->count();
            $cate->all_number = Bonus::where('name', $cate->name)->count();
        }
        return view("bonuses.statistics")->with('bonuses', $cates);
    }

    public function delByName()
    {
        $name = \request('name');
        Bonus::where('name',$name)->delete();
        Flash::success('Bonus deleted successfully.');
        return redirect(route('bonuses.statistics'));
    }
}
